<?php

namespace App\Http\Controllers\Api;

use App\Competition;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CompetitionController extends Controller
{
    public function index(Request $request)
    {
        $com = Competition::all();
        $filter = collect();
        foreach ($com as $key) {
            if ($key->status == 1)
                $filter->push($key);
        }
        if (is_null($request->search))
            return [
                'status' => 'success',
                'data_size' => $filter->count(),
                'data' => $filter
            ];
        else{
            return Competition::where([['status',1],[
                'title','like',"%{$request->search}%"
            ]])->get();
        }
    }

    public function detail($id)
    {
        return [
            'status' => 'success',
            'data' => Competition::find($id)
        ];
    }

}
